package com.hung.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClientDemo {
    public static void main(String[] args) {
        InetSocketAddress address = new InetSocketAddress("127.0.0.1",8765);
        SocketChannel sc =null;
        ByteBuffer byteBuffer = null;
        byteBuffer = ByteBuffer.allocate(1024);

        try{
            sc = SocketChannel.open();
            sc.connect(address);
            while(true) {
                byte[] bytes = new byte[1024];

                System.in.read(bytes);

                byteBuffer.put(bytes);
                byteBuffer.flip();

                sc.write(byteBuffer);
                byteBuffer.clear();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(sc!=null) {
                    sc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
